using System;
using System.Windows.Forms;
using System.IO;
using System.Threading;
using System.Collections.Generic;
using System.Collections;
using System.Text.RegularExpressions;
using System.Text;
using GeniePlugin.Interfaces;
using System.Runtime.Serialization.Formatters.Binary;
using System.Data.SQLite;
using System.Data;
using GeniePlugin;

namespace CombatCalcPlugin
{
    public class Plugin : GeniePlugin.Interfaces.IPlugin
    {
        #region IPlugin Members
        public IHost _host;                             //Required for plugin
        public System.Windows.Forms.Form _parent;       //Required for plugin

        public List<string> alValidParams = new List<string>();

        void IPlugin.Initialize(IHost Host)
        {
            _host = Host;
            this.Enabled = true;

            if (this._host.get_Variable("combatCalc.Evasion") == null)
                this._host.set_Variable("combatCalc.Evasion", "1");

            if (this._host.get_Variable("combatCalc.Shield") == null)
                this._host.set_Variable("combatCalc.Shield", "1");

            if (this._host.get_Variable("combatCalc.Parry") == null)
                this._host.set_Variable("combatCalc.Parry", "1");

            alValidParams.Add("backstab");
            alValidParams.Add("bs");
            alValidParams.Add("skin");
            alValidParams.Add("skinable");
            alValidParams.Add("boxes");
            alValidParams.Add("undead");

        }

        string IPlugin.Name
        {
            get { return "Combat Calculator"; }
            //get { throw new NotImplementedException(); }
        }

        void IPlugin.ParentClosing()
        {
            //throw new NotImplementedException();
        }

        string IPlugin.ParseText(string text, string Window)
        {
            //throw new NotImplementedException();
           
            return text;
        }

        string IPlugin.ParseInput(string Text)
        {
            if (Text.ToLower().StartsWith("/combatcalc"))
            {
                //this._host.SendText("exp all");
                double iAvgDefenses = 0;

                Regex exp = new Regex(" ");
                int space = exp.Matches(Text.Trim()).Count;

                if(space > 0)
                {
                    //string[] param = Text.Substring(Text.IndexOf(" ")).Split(' ');
                    //foreach (string s in param)
                    //    if (alValidParams.Contains(s))

                }

                int iSkillCount = 0;

                if (this._host.get_Variable("combatCalc.Evasion") == "1")
                {
                    iSkillCount++;
                    iAvgDefenses += Convert.ToDouble(this._host.get_Variable("Evasion.Ranks"));
                }

                if (this._host.get_Variable("combatCalc.Shield") == "1")
                {
                    iSkillCount++;
                    iAvgDefenses += Convert.ToDouble(this._host.get_Variable("Shield_Usage.Ranks"));
                }

                if (this._host.get_Variable("combatCalc.Parry") == "1")
                {
                    iSkillCount++;
                    iAvgDefenses += Convert.ToDouble(this._host.get_Variable("Parry_Ability.Ranks"));
                }


                iAvgDefenses = iAvgDefenses / iSkillCount;
                
                Dictionary<string, object> i = new Dictionary<string, object>();
                StreamReader streamReader = new StreamReader(Application.StartupPath + @"\Plugins\Critters.json");
                string sJson = streamReader.ReadToEnd();
                streamReader.Close();

                fastJSON.JSON.Instance.UseSerializerExtension = false;
                //var newobj = fastJSON.JSON.Instance.ToObject(text);
                Dictionary<string, object> al = (Dictionary<string, object>)fastJSON.JSON.Instance.Parse(sJson);
                ArrayList alItems = (ArrayList)al["items"];

                StringBuilder sb = new StringBuilder();
                string format = "{0,-50}{1,-20}{2,-20}{3,-150}";
                foreach (Dictionary<string, object> alItem in alItems)
                    if (
                        int.Parse(((ArrayList)alItem["max_cap"])[0].ToString()) > iAvgDefenses && 
                        int.Parse(((ArrayList)alItem["min_cap"])[0].ToString()) < iAvgDefenses && 
                        int.Parse(((ArrayList)alItem["max_cap"])[0].ToString()) != 0
                        )
                    {
                        string map = string.Empty;
                        try
                        {
                            map = ((ArrayList)alItem["map"])[0].ToString();
                        }
                        catch (Exception ex)
                        {

                        }
                        sb.AppendFormat(format, "Name: " + ((string)alItem["label"]), "Min Rank: " + ((ArrayList)alItem["min_cap"])[0].ToString(), "Max Rank: " + ((ArrayList)alItem["max_cap"])[0].ToString(), "Location: " + map);
                        sb.AppendLine();
                    }

                this._host.EchoText(sb.ToString());
            }



            
            //throw new NotImplementedException();
            return Text;
        }

        void IPlugin.ParseXML(string XML)
        {
            //throw new NotImplementedException();
        }

        void IPlugin.Show()
        {
            //throw new NotImplementedException();
            Form1 f1 = new Form1(ref this._host);
            f1.Parent = this._parent;
            f1.Show();
        }

        void IPlugin.VariableChanged(string Variable)
        {
            //throw new NotImplementedException();
        }

        string IPlugin.Version
        {
            get { return "First Version";}
        }


        public bool Enabled
        {
            get;
            set;
        }

        #endregion

        #region IPlugin Members

        public string Author
        {
            get { return "Rico Wahl"; }
        }

        public string Description
        {
            get { return "Combat Calculator"; }
        }

        #endregion
    }
}
